﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VectorHelper;

namespace _2D碰撞
{
    public partial class MianForm : Form
    {
        public MianForm()
        {
            InitializeComponent();
        }

        private static Color ball_color = Color.Red;
        private static Color P_color = Color.Green;
        private static Color bg_color = Color.Black;

        //多边形属性
        private static PointF A = new PointF(100, 50);
        private static PointF B = new PointF(200, 100);
        private static PointF C = new PointF(200, 200);
        private static PointF D = new PointF(50, 100);
        private static PointF[] points = { A, B, C, D };


        private static PointF S1 = new PointF(10.0f, 10.0f);
        private static PointF S2 = new PointF(290.0f, 10.0f);
        private static PointF S3 = new PointF(290.0f, 230.0f);
        private static PointF S4 = new PointF(10.0f, 230.0f);

        const float v = 5.0f;//速度不能超过半径，不然会冲出范围
        Ball ball = new Ball(120.0f, 120.0f, 5.0f, new Vector(2.0f * v, 0.0f * v));
        int i = 0;
        private void timer1_Tick(object sender, EventArgs e)
        {

            //清除原来的小球
            PaintBall(ball, bg_color);

            //判断是否相撞，如果相撞就改变方向
            List<PointF[]> lines = new List<PointF[]>() { 
            new PointF[] { A, B },
            new PointF[] { B, C },
            new PointF[] { C, D },
            new PointF[] { D, A },

            new PointF[] { S1, S2 },
            new PointF[] { S2, S3 },
            new PointF[] { S3, S4 },
            new PointF[] { S4, S1 },
            };
            lines.Add(new PointF[] { A, B });
            var result = Helper.CollisionTest(lines, ball);
            if (result.isCollision)
            {
                i++;
                label1.Text = "第" + i + "次碰撞";
                //改变方向
                ball = Helper.Rebound(ball, result.Plst);
            }
            //重画小球
            PaintBall(Helper.BallMove(ball), ball_color);




            //重画多边形跟文字
            PaintPolygo();
        }

        /// <summary>
        /// 画小球
        /// </summary>
        /// <param name="ball"></param>
        /// <param name="color"></param>
        private void PaintBall(Ball ball, Color color)
        {
            using (Pen p = new Pen(color))
            {
                Graphics g = this.panel1.CreateGraphics();
                g.DrawEllipse(p, ball.X - ball.R, ball.Y - ball.R, ball.R * 2, ball.R * 2);
            }
        }


        /// <summary>
        /// 画多边形
        /// </summary>
        private void PaintPolygo()
        {
            // 写字
            PaintText();
            //画多边形
            using (Pen p = new Pen(P_color))
            {
                Graphics g = this.panel1.CreateGraphics();
                g.DrawPolygon(p, points);
            }
            using (Pen p = new Pen(P_color))
            {
                Graphics g = this.panel1.CreateGraphics();
                g.DrawPolygon(p, new PointF[] { S1, S2, S3, S4 });
            }
        }

        /// <summary>
        /// 写字
        /// </summary>
        private void PaintText()
        {
            using (Brush b = new SolidBrush(P_color))
            {
                Graphics g = this.panel1.CreateGraphics();
                g.DrawString("A", new Font("宋体", 10, FontStyle.Regular), b, A);
                g.DrawString("B", new Font("宋体", 10, FontStyle.Regular), b, B);
                g.DrawString("C", new Font("宋体", 10, FontStyle.Regular), b, C);
                g.DrawString("D", new Font("宋体", 10, FontStyle.Regular), b, D);
            }
        }

        /// <summary>
        /// 鼠标事件
        /// </summary> 
        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            switch (e.Button)
            {
                case MouseButtons.Left:
                    //逆时针旋转90°
                    ball.velocity = new Vector(ball.velocity.Y, -ball.velocity.X);
                    break;
                case MouseButtons.Right:
                    //顺时针旋转90°
                    ball.velocity = new Vector(-ball.velocity.Y, ball.velocity.X);
                    break;
                default:
                    break;
            }

        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            int i = 0;
            float x = 0.0f;
            float y = 0.0f;
            if (float.TryParse(this.tbxX.Text, out x))
            {
                i++;
            }
            if (float.TryParse(this.tbxY.Text, out y))
            {
                i++;
            }
            if (2 == i)
            {
                //添加一个小球


            }
        }



    }
}
